package blt.app_core.http;


import java.io.File;
import java.util.Map;

import blt.app_core.http.callback.HttpCallback;
import blt.app_core.http.request.LoginRequest;
import blt.app_core.utils.FileHelper;
import blt.app_core.utils.UriProdiver;
import okhttp3.Call;
import okhttp3.OkHttpClient;

/**
 * xugaopan
 * 2016/12/13.
 * 封装公共参数
 */
public class Http extends HttpCore {
    private static Http mInstance;

    public Http() {
        super(new DefaultRequestBuilder());//初始化okhttp对象
        OkHttpClient.Builder builder = okHttpClient.newBuilder();
        //请求超时的时间,下面这2段代码很容易引起链接超时，慎重！
//        builder.connectTimeout(10 * 1000, TimeUnit.MICROSECONDS).build();
//        //添加拦截器，用于拦截请求之前的数据，来做上传进度控制
        builder.addInterceptor(interceptToProgressResponse());
        okHttpClient = builder.build();
    }

    public static Http getInstance() {
        if (mInstance == null) {
            synchronized (Http.class) {
                if (mInstance == null) {
                    mInstance = new Http();
                }
            }
        }
        return mInstance;
    }


    public static OkHttpClient getClient() {
        return getInstance().okHttpClient;
    }


    /**
     * 异步GET
     **/
    public static void get(String url, HttpCallback callback) {
        getAsync(url, callback, null, null);
    }

    /**
     * 带参数
     *
     * @param url
     * @param callback
     * @param strParams
     */
    public static void get(String url, Map<String, String> strParams, HttpCallback callback) {
        getAsync(url, callback, null, strParams);
    }

    private static void getAsync(String url, HttpCallback callback, Object tag, Map<String, String> strParams) {
        getInstance().executeGetAsync(callback, url, tag, strParams);
    }

    /**
     * ----------- post--------
     */
    public static void post(String url, Map<String, String> params, HttpCallback callback) {
        postAsync(UriProdiver.BASE_URL + url, params, callback, null);
    }

    public static void post(String url, HttpCallback callback) {
        postAsync(url, null, callback, null);
    }

    /**
     * @param url      请求地址
     * @param params   参数
     * @param callback 回调
     * @param tag      标记用于取消请求
     */
    private static void postAsync(String url, Map<String, String> params, HttpCallback callback, Object tag) {
        getInstance().executePost(url, params, callback, tag);
    }


    /**
     * upload
     **/

    public static Call upload(String url, String path, HttpCallback callback) {
        return uploadAsync(url, path, null, null, callback);
    }

    public static Call upload(String url, String path, Map<String, String> params, HttpCallback callback) {
        return uploadAsync(url, path, params, null, callback);
    }

    public static Call uploadAsync(String url, String path, Map<String, String> params, Object tag, HttpCallback callback) {
        return getInstance().executeUploadAsync(url, path, params, tag, callback);
    }

    /**
     * @param url      下载地址
     * @param fileDir  文件存储目录
     * @param callback
     */
    public static void download(String url, String fileDir, HttpCallback<File> callback) {
        downloadAsync(url, fileDir, null, null, callback, null);
    }

    /**
     * @param url      下载地址
     * @param fileDir  文件存储目录
     * @param fileName 文件名称
     * @param tag      取消下载的标记
     * @param callback 下载回调
     * @param params   下载请求参数
     */
    public static void downloadAsync(String url, String fileDir, String fileName,
                                     Object tag, HttpCallback<File> callback, Map<String, String> params) {
        downloadAsync(url, fileDir, fileName, tag, callback, METHOD_GET, params);
    }

    private static void downloadAsync(String url, String fileDir, String fileName, Object tag, HttpCallback<File> callback, int method, Map<String, String> params) {
        downloadAsync(url, FileHelper.getFile(fileDir, fileName, url), tag, callback, method, params);
    }

    public static void downloadAsync(String url, File outFile, Object tag, HttpCallback<File> callback, int method, Map<String, String> params) {
        getInstance().executeDownloadAsync(callback, url, outFile, tag, method, params);
    }





}
